import 'package:detect/notifier/location.dart';
import 'package:detect/pages/home.dart';
import 'package:flutter/material.dart';
import 'package:flutter_color_plugin/flutter_color_plugin.dart';
import 'package:detect/notifier/network_state.dart';
import 'package:detect/notifier/offline_map_download.dart';
import 'package:detect/utils/global.dart';
import 'package:detect/utils/image_util.dart';
import 'package:detect/widgets/future_widget.dart';
import 'package:provider/provider.dart';

class DownloadMap extends StatefulWidget {
  const DownloadMap({super.key});

  @override
  State<DownloadMap> createState() => _DownloadMapState();
}

class _DownloadMapState extends State<DownloadMap> {
  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      context
          .read<OfflineMapDownloadNotifier>()
          .downloadend
          .then((value) => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const Home())));
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) =>
      Scaffold(
        body: Consumer2<LocationNotifier, NetworkStateNotifier>(
          builder: (context, locationNotifier, networkStateNotifier, child) {
            if (networkStateNotifier.state.hasNetwork) {
              return FutureWidget<bool>(
                future: locationNotifier.waitLocation,
                loadingDesc: "等待获取位置信息",
                emptyTitle: "定位失败",
                emptySubTitle: "暂时无法获取您的位置信息",
                doneBuilder: (context, snapshot) {
                  context.read<OfflineMapDownloadNotifier>().download(locationNotifier.city!);
                  return Align(
                    child: Padding(
                      padding: EdgeInsets.all(Global.ds(20)),
                      child: Consumer<OfflineMapDownloadNotifier>(
                        builder: (context, notifier, child) {
                          int progress = notifier.progress?.completeCode ?? 0;
                          String desc;
                          if (notifier.state == 2) {
                            desc = "离线地图已下载完成";
                          } else if (notifier.state == 1) {
                            if (notifier.progress?.status == 0) {
                              desc = "正在下载离线地图";
                            } else if (notifier.progress?.status == 1) {
                              desc = "正在解压离线地图";
                            } else {
                              desc = "离线地图";
                            }
                          } else {
                            desc = "等待下载离线地图";
                          }
                          return Column(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              ImageUtil.icon("static/images/icon/map.png", size: Global.ds(42), color: ColorUtil.color("#10BEFF")),
                              Divider(height: Global.ds(20), color: Colors.transparent),
                              SizedBox(
                                width: Global.ds(200),
                                height: Global.ds(4),
                                child: LinearProgressIndicator(
                                  value: progress / 100,
                                  color: ColorUtil.color("#10BEFF"),
                                  backgroundColor: ColorUtil.color("#D8D8D8"),
                                ),
                              ),
                              Divider(height: Global.ds(5), color: Colors.transparent),
                              Text("$progress%", style: TextStyle(fontSize: Global.ds(14), color: ColorUtil.color("#10BEFF"))),
                              Divider(height: Global.ds(15), color: Colors.transparent),
                              Text(desc, softWrap: true, style: TextStyle(fontSize: Global.ds(14), color: ColorUtil.color("#3D3D3D"))),
                              Divider(height: Global.ds(5), color: Colors.transparent),
                              Text(
                                locationNotifier.city ?? '',
                                style: TextStyle(fontSize: Global.ds(18), color: ColorUtil.color("#3D3D3D"), fontWeight: FontWeight.bold),
                              ),
                            ],
                          );
                        },
                      ),
                    ),
                  );
                },
              );
            } else {
              return Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  Container(
                    alignment: Alignment.center,
                    child: ImageUtil.asset("static/images/offline.png", size: Global.ds(160)),
                  ),
                  Container(
                    alignment: Alignment.center,
                    padding: EdgeInsets.only(bottom: Global.ds(5)),
                    margin: EdgeInsets.only(top: Global.ds(20)),
                    child: Text(
                      "网络请求失败",
                      textAlign: TextAlign.center,
                      style: TextStyle(color: ColorUtil.color("#666666"), fontSize: Global.ds(16)),
                    ),
                  ),
                  Container(
                    alignment: Alignment.center,
                    padding: EdgeInsets.only(top: Global.ds(5)),
                    margin: EdgeInsets.only(bottom: Global.ds(20)),
                    child: Text(
                      "联网后将自动下载离线地图",
                      textAlign: TextAlign.center,
                      style: TextStyle(color: ColorUtil.color("#999999"), fontSize: Global.ds(12)),
                    ),
                  ),
                ],
              );
            }
          },
        ),
      );
}
